package com.itcpeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcpeng.dao.MemberDao;
import com.itcpeng.dao.OrderDao;
import com.itcpeng.dao.PackageDao;
import com.itcpeng.service.ReportService;
import com.itcpeng.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private PackageDao packageDao;


    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    //查询套餐占比数据
    @Override
    public List<Map<String, Object>> getPackageReport() {
        return packageDao.findPackageCount();
    }

    //打印pol报表
    @Override
    public Map<String, Object> getBusinessReportData() {
        //当前日期
        Date newDate = new Date();
        String reportDate = DateUtils.dataToString(newDate, DateUtils.YMD);
        //星期一的日期
        String Monday = DateUtils.dataToString(DateUtils.getThisWeekMonday(), DateUtils.YMD);
        //星期天
        String sunday = DateUtils.dataToString(DateUtils.getSundayOfThisWeek(), DateUtils.YMD);
        //一号的日期
        String firstday = DateUtils.dataToString(DateUtils.getFirstDay4ThisMonth(), DateUtils.YMD);
        //本月最后一天的日期
        String lastday = DateUtils.dataToString(DateUtils.getLastDay4ThisMonth(), DateUtils.YMD);
        //今日新增会员
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(Monday);
        //每月新增日期
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstday);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(Monday, sunday);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(Monday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountBetweenDate(firstday, lastday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstday);

        //热门套餐
        List<Map<String, Object>> hotPackage = orderDao.findHotPackage();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reportDate", reportDate);
        map.put("todayNewMember", todayNewMember);
        map.put("totalMember", totalMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("hotPackage", hotPackage);
        return map;
    }

    //获取会员数量
    @Override
    public Map<String, Object> getRangeMemberReport(String date, String date01) {
        try {
            Map<String, Object> map = new HashMap<>();
            String minDate = date;
            String maxDate = date01;
            //存储月份列表
            List<String> months = getMonthBetween(minDate, maxDate);
            //存储月份对应会员数的列表
            List<Integer> memberCount = new ArrayList<>();
            //获取月份对应的会员数
            for (String month : months) {
                String monStr = month + ".31";
                memberCount.add(memberDao.findCountByMonth(monStr));
            }
            map.put("months", months);
            map.put("memberCount", memberCount);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
}

    private List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        min = null;
        max = null;
        curr = null;
        return result;
    }

}
